const _my = require("../../__antmove/api/index.js")(my);
const wx = _my;
let disp = require("../../utils/broadcast");
Page({
    data: {
        username: {
            your: "",
            myName:"",
            groupId:''
        },
        baseUrl: 'http://127.0.0.1:8001',
        promptId: ''
    },
    // options = 系统传入的 url 参数
    onLoad(options) {
        let username = JSON.parse(options.username);
        this.setData({
            username: username
        });
        wx.setNavigationBarTitle({
            title: username.your
        });
    },
    onUnload() {
        disp.fire("em.chatroom.leave");
    },
    onPullDownRefresh: function() {
        wx.showNavigationBarLoading();
        this.selectComponent("#groupchat").getMore();
        // 停止下拉动作
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
    },
    handleSearch(e) {
      console.log('search', e.detail.value);
      this.setData({
        search: e.detail.value,
      });
    },


    sendMessage() {
      my.request({
        url: this.data.baseUrl + '/message/sendMessage',
        method: 'POST',
        data: {
          groupOrOwn: 0,
          content: this.data.search,
          contentType: 0,
          groupId: this.data.username.groupId, 
          userId: this.data.username.myName
        },
        headers: {
          'content-type': 'application/json', //默认值
        },
        dataType: 'json',
        success: function (res) {
          my.alert({ content: 'success' });
        },
        fail: function (error) {
          console.error('fail: ', JSON.stringify(error));
        },
        complete: function (res) {
          my.hideLoading();
          my.alert({ content: 'complete' });
        },
      });
    },

    askForSummary() {
      my.request({
        url: this.data.baseUrl + '/bot/summary',
        method: 'POST',
        data: {
          groupId: this.data.username.groupId, 
          offset: 30
        },
        headers: {
          'content-type': 'application/json', //默认值
        },
        dataType: 'json',
        success: function (res) {
          my.alert({ content: 'success' });
        },
        fail: function (error) {
          console.error('fail: ', JSON.stringify(error));
        },
        complete: function (res) {
          my.hideLoading();
          my.alert({ content: 'complete' });
        },
      });
    },

    askBot() {
      my.request({
        url: this.data.baseUrl + '/bot/ask',
        method: 'POST',
        data: {
          msg: '',
          chatId: '',
          toUserId: 'user_Bot',
          promptId: ''
        },
        headers: {
          'content-type': 'application/json', //默认值
        },
        dataType: 'json',
        success: function (res) {
          my.alert({ content: 'success' });
        },
        fail: function (error) {
          console.error('fail: ', JSON.stringify(error));
        },
        complete: function (res) {
          my.hideLoading();
          my.alert({ content: 'complete' });
        },
      });
    },

    addBot() {
      my.request({
        url: this.data.baseUrl + '/bot/add',
        method: 'POST',
        data: {
          groupId: this.data.username.groupId, 
          userId: 'user_Bot'
        },
        headers: {
          'content-type': 'application/json', //默认值
        },
        dataType: 'json',
        success: function (res) {
          my.alert({ content: 'success' });
        },
        fail: function (error) {
          console.error('fail: ', JSON.stringify(error));
        },
        complete: function (res) {
          my.hideLoading();
          my.alert({ content: 'complete' });
        },
      });
    },

    promptList() {
      my.request({
        url: this.data.baseUrl + '/ai_prompt/list',
        method: 'GET',
        data: {
        },
        headers: {
          'content-type': 'application/json', //默认值
        },
        dataType: 'json',
        success: function (res) {
          my.alert({ content: 'success' });
        },
        fail: function (error) {
          console.error('fail: ', JSON.stringify(error));
        },
        complete: function (res) {
          my.hideLoading();
          my.alert({ content: 'complete' });
        },
      });
    },

    doneSearch() {
      console.log('doneSearch', this.data.search);
      my.hideKeyboard();
      // conn.send(msg).then(()=>{
      //   console.log("Send message success");
      // }).catch((e)=>{
      //   console.log("Send message fail");
      // });
      // 向自己的后端发消息
      // this.askForSummary();
      this.promptList();
    }
});
